package com.baishancloud.log

import cn.hutool.http.HttpUtil
import cn.hutool.json.{JSONArray, JSONObject, JSONUtil}
import com.baishancloud.log.directories.FetchLatestDomainId
import com.baishancloud.log.directories.StreamingDirectoriesAggregate.{LogKey, LogValue}
import org.junit.Test

import java.util
import java.util.Objects
import scala.collection.mutable
import scala.collection.mutable.ListBuffer

class AppTestS {

  @Test
  def test1(): Unit = {
    val url: String = "http://portal-api.bs58i.baishancdnx.com/apix"
    val logFormat: util.Map[String, Object] = new util.HashMap[String, Object]()

    //访问logformat配置数据http参数
    logFormat.put("token", "cddc83db84cebae8023ef993da57a869")
    logFormat.put("method", "domain.get_configs")
    val logFormatParams: util.Map[String, Object] = new util.HashMap[String, Object]()
    val funNames: util.ArrayList[String] = new util.ArrayList[String]()
    funNames.add("logformat")
    logFormatParams.put("fun_names", funNames)
    logFormat.put("params", logFormatParams)

    val postData: String = HttpUtil.post(url, JSONUtil.toJsonStr(logFormat), 60000)
    val result: mutable.Map[String, Int] = new mutable.HashMap[String, Int]()

    val jsonObj: JSONObject = JSONUtil.parseObj(postData)
    if (jsonObj.containsKey("errno") && jsonObj.getInt("errno") == 0) {
      val data: JSONObject = jsonObj.getJSONObject("data")
      val iter: util.Iterator[util.Map.Entry[String, AnyRef]] = data.entrySet().iterator()
      while (iter.hasNext) {
        val next: util.Map.Entry[String, AnyRef] = iter.next()
        val obj: JSONObject = JSONUtil.parseObj(next.getValue)
        result.put(obj.getStr("domain"), obj.getInt("id"))
      }
    }

    println(result)
  }

  @Test
  def test2(): Unit = {
    val url: String = "http://portal-api.bs58i.baishancdnx.com/apix"
    val catalog: util.Map[String, Object] = new util.HashMap[String, Object]()
    //访问maocloud-catlog数据http参数
    catalog.put("token", "cddc83db84cebae8023ef993da57a869")
    catalog.put("method", "domain.get")
    val catalogParams: util.HashMap[String, String] = new util.HashMap[String, String]()
    catalogParams.put("account", "maocloud-catlog")
    catalog.put("params", catalogParams)

    val accountDomainPost: String = HttpUtil.post(url, JSONUtil.toJsonStr(catalog), 60000)
    val accountDomains: ListBuffer[String] = new ListBuffer[String]()
    val accountDomainJsonObj: JSONObject = JSONUtil.parseObj(accountDomainPost)
    if (accountDomainJsonObj.containsKey("errno") && accountDomainJsonObj.getInt("errno") == 0) {
      val jsonArray: JSONArray = accountDomainJsonObj.getJSONArray("data")
      val iter: util.Iterator[AnyRef] = jsonArray.iterator()
      while (iter.hasNext) {
        accountDomains.+=(JSONUtil.parseObj(iter.next()).getStr("domain"))
      }
    }
    println(accountDomains)
  }

  @Test
  def test3(): Unit = {
    val s: String = "/$domain/$year-$month-$day/$hour/"
    println("sdfds\\dgdsfg".replace("\\", ""))
  }


  @Test
  def test4(): Unit = {
    val k1 = LogKey("0", 0, 0, "0")
    val k2 = LogKey("0", 1, 0, "0")
    print(Objects.equals(k1, k2))
  }

  @Test
  def test5(): Unit = {
    val v1 = LogValue(0, 0)
    val v2 = LogValue(1, 0)
    print(Objects.equals(v1, v2))
  }

  @Test
  def test6(): Unit = {
    val domainId: mutable.Map[String, Int] = mutable.Map[String, Int]()
    FetchLatestDomainId.core(domainId)
    println(domainId.size)
    println(domainId.toString())
  }

}
